<?php
/**
 * @author Matteo Vigoni <mattevigo@gmail.com>
 * @package INTVWeb
 * @version 1.0
 *
 * Classe Event
 */
require_once($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."config_dbentity.php");
require_once(DBENTITY);

import("core.blog.Blogpost");
import("core.Extension");
import("core.social.FacebookInterface");
import("core.social.TwitterInterface");

class Release extends Blogpost implements FacebookInterface, TwitterInterface
{
	// Parent
	const PARENT_TABLE = 'blogposts';
	const PARENT_KEY = 'blogpost_id';
	const PARENT_CLASS = 'Blogpost';

	// tabella
	const TABLE = 'blogposts_releases';
	const PRIMARY_KEY = 'release_id';

	// Table
	var $table = 'blogposts_releases';
	var $primary_key = 'release_id';

	const AUTH_GROUP = 'blog';

	/**
	 * Costruttore
	 *
	 * @param $db
	 * @param $id
	 * @return unknown_type
	 */
	public function __construct( DB $db, $table=NULL, $prymary_key=NULL, Seed $seed=NULL, $event_id=NULL )
	{
		if( $seed != NULL )
		{
			$this->setSeed( &$seed );	// set the seed object
			
			$blogpost = $seed->getObject( "core.blog.Blogpost" );	// get the parent
			$this->setParent( $blogpost );							// set the parent
			
			$table = $db->prefix().$this->table;
			
			$sql = "SELECT release_id ".
					"FROM $table ".
					"WHERE blogpost_id={$blogpost->getId()}";
			
			$result = $db->query( $sql );
			$row = mysql_fetch_assoc( $result );
			
			parent::__construct($db, NULL, NULL, NULL, $row['release_id']);
		}
		else
		{
			parent::__construct($db, NULL, NULL, NULL, $event_id);
		}
	}

	// Actions ////////////////////////////////////////////////////////////////////////////

	public function checkInput( $input, $err )
	{
		return true;
	}

	public function doPost( User $user=NULL, Session $session=NULL )
	{
		global $_POST;
		$err = "";

		// User ////////////////////////////////////////////////////////////////////////
		$usr = NULL;
		if( $user == NULL )
		{
			if( $this->getUser() != NULL )
			{
				$usr = $this->getUser();

				if( !$usr->isInGroup( self::AUTH_GROUP ))
				{
					throw new DBEntityException("You don't have the rights");
				}
			}
			else
			{
				throw new DBEntityException("No User specified");
			}
		}
		else
		{
			$usr = $user;
		}

		$this->set('user_id', $usr->getId());	# set user_id in the object

		// Sets /////////////////////////////////////////////////////////////////////////

		if( $this->checkInput( &$_POST, &$err ))
		{
			$blogpost = NULL;
			if( isset($_POST['post_id']) || strcmp($_POST['post_id'], "") == 0 )
			{
				$blogpost = new Blogpost( $this->getDB(), $_POST['post_id'] );
			}
			else
			{
				$blogpost = new Blogpost( $this->getDB() );
			}

			$blogpost->doPost( &$usr, &$session );
			$blogpost->store();

			$this->set('post_id', $blogpost->getId());

			$this->set('event_place', $_POST['event_place']);
			$this->set('event_address', $_POST['event_address']);
			$this->set('event_start', $_POST['event_start']);

			//TODO picture uploading

			if( isset($_POST['event_end']) ) $this->set('event_start', $_POST['event_start']);
			if( isset($_POST['event_link']) ) $this->set('event_link', $_POST['event_link']);
			if( isset($_POST['event_youtube']) ) $this->set('event_youtube', $_POST['event_youtube']);
			if( isset($_POST['event_googlemap']) ) $this->set('event_googlemap', $_POST['event_googlemap']);
		}
		else
		{
			throw new InvalidInputException( $err );
		}
	}

	// STATIC FUNCTIONS ///////////////////////////////////////////////////////////////////

	/**
	 *
	 * @param Blogpost $blogpost
	 * @return Event
	 */
	public static function getEventFromBlogpost( Blogpost $blogpost )
	{
		$table = $blogpost->getDB()->prefix() . self::TABLE;

		$sql = "SELECT event_id FROM $table WHERE blogpost_id={$blogpost->getId()} LIMIT 0, 1 ";

		$result = $db->query( $sql );
		$row = mysql_fetch_row( $result );

		$event = new Event( &$db, $row['event_id']);

		$event->addAssoc( $blogpost->fetchAssoc() );

		return $event;
	}

	/**
	 * Restituisce tutti gli eventi
	 *
	 * @return array di Event
	 */
	public static function getAll(){}

	/**
	 * Restituisce gli eventi passati
	 *
	 * @return array di Event passati
	 */
	public static function getExpired(){}

	/**
	 * Restituisce gli eventi in corso nel momento
	 *
	 * @return array di Event in corso
	 */
	public static function getActived(){}

	/**
	 * Restituisce gli eventi futuri
	 *
	 * @return array di Event futuri
	 */
	public static function getFutures(){}

	// SOCIAL ///////////////////////////////////////////////////////////////////////////////////

	///////// Facebook Functions ///////////

	public function setFacebook( Facebook $f )
	{
		$this->set('facebook', $f);
	}

	public function updateStatus( User $usr )
	{
		//throw new NotImplementedException("function updateStatus() not implemented");
		$this->get('facebook');
	}

	////////// Twitter Functions ///////////

	public function getTwitterEmail()
	{
		throw new NotImplementedException("function getTwitterEmail() not implemented");
	}

	public function getTwitterUsername()
	{
		throw new NotImplementedException("function getTwitterUsername() not implemented");
	}

	public function getTwitterPassword()
	{
		throw new NotImplementedException("function getTwitterPassword() not implemented");
	}

	public function tweet( User $usr )
	{
		throw new NotImplementedException("function tweet() not implemented");
	}
}